﻿Public Class clsTaskStep
    Public Function SelectTaskStep1(task_step_status As String, task_type As String, pick As String, ty As Boolean) As DataTable
        Dim szSql As String = ""
        szSql &= "select agv.task.type,agv.task.start_time,agv.task.end_time,agv.task.tm,agv.task_step.*,sf.t_prod_spec.prod_spec_name from agv.task"
        szSql &= " left join agv.task_step on agv.task.sys_id=agv.task_step.task_id"
        szSql &= " left join sf.t_prod_spec on agv.task.prod_spec_id=sf.t_prod_spec.prod_spec_id"
        szSql &= " where agv.task_step.status='"
        szSql &= task_step_status
        szSql &= "' and agv.task.type in ("
        szSql &= task_type
        szSql &= ") "
        szSql &= "and agv.task_step.pick='"
        szSql &= pick
        szSql &= "' and agv.task_step.previous_id is null"
        szSql &= " and agv.task_step.update_time >= current_date "
        szSql &= " order by agv.task_step.update_time "
        If ty Then
            szSql &= "limit 1"
        End If
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1)
        If IsNothing(dt) Then
            SelectTaskStep1 = New DataTable
        Else
            SelectTaskStep1 = dt
        End If
    End Function

    Public Function updateTaskStep(agv As String, state As String, state_task As String, id As String, task_id As String, prod_spec_id As String) As Integer
        Dim szSql As String = String.Empty
        Dim rtn As Integer
        DBacs.BeginTransaction()
        Try
            szSql &= "update agv.task_step set agv = ?"
            szSql &= "where task_id = ?"
            rtn = DBacs.ExecuteUpdateSql(szSql, -1, agv, task_id)
            '更新新任务步骤状态
            szSql = String.Empty
            szSql &= "update agv.task_step set prod_spec_id=?, status = ?, update_time = (SELECT CURRENT_TIMESTAMP(6))"
            szSql &= "where sys_id = ?"
            rtn = DBacs.ExecuteUpdateSql(szSql, -1, prod_spec_id, state, id)
            '插入state表状态
            szSql = String.Empty
            szSql &= "insert into agv.task_step_state(task_step_id,state,update_time) values ('" & id & "','" & state & "',(SELECT CURRENT_TIMESTAMP(6)))"
            rtn = DBacs.ExecuteUpdateSql(szSql, -1)
            '更新任务表状态
            szSql = String.Empty
            szSql &= "update agv.task set status = ?,update_time = (SELECT CURRENT_TIMESTAMP(6)) "
            szSql &= "where sys_id=?"
            rtn = DBacs.ExecuteUpdateSql(szSql, -1, state_task, task_id)
            DBacs.CommitTransaction()
        Catch ex As Exception
            DBacs.RollbackTransaction()
            rtn = -2
        End Try
        Return rtn
    End Function

End Class
